home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / source / ncsat.cpt / Telnet2.5 final / main / dnr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-12-23  |  3.8 KB  |  188 lines

  1. /*     DNR.c - DNR library for MPW
  2.  
  3.     (c) Copyright 1988 by Apple Computer.  All rights reserved
  4.     
  5. */
  6.  
  7. #ifdef USEDUMP
  8. # pragma load "Socket.dump"
  9. #else
  10. # include <OSUtils.h>
  11. # include <Errors.h>
  12. # include <Files.h>
  13. # include <Resources.h>
  14. # include <Memory.h>
  15. #endif
  16.  
  17. #define OPENRESOLVER    1
  18. #define CLOSERESOLVER    2
  19. #define STRTOADDR        3
  20. #define    ADDRTOSTR        4
  21. #define    ENUMCACHE        5
  22. #define ADDRTONAME        6
  23.  
  24. Handle codeHndl = nil;
  25.  
  26. typedef OSErr (*OSErrProcPtr)();
  27. OSErrProcPtr dnr = nil;
  28.  
  29.  
  30. /* OpenOurRF is called to open the MacTCP driver resources */
  31.  
  32. short OpenOurRF()
  33. {
  34.     SysEnvRec info;
  35.     HParamBlockRec fi;
  36.     Str255 filename;
  37.     short curVRefNum;
  38.     WDPBRec pb;
  39.     int status;
  40.  
  41.     SysEnvirons(1, &info);
  42.  
  43.     fi.fileParam.ioCompletion = nil;
  44.     fi.fileParam.ioNamePtr = &filename;
  45.     fi.fileParam.ioVRefNum = info.sysVRefNum;
  46.     fi.fileParam.ioDirID = 0;
  47.     fi.fileParam.ioFDirIndex = 1;
  48.     
  49.     while (PBHGetFInfo(&fi, false) == noErr) {
  50.         /* scan system folder for driver resource files of specific type & creator */
  51.         if (fi.fileParam.ioFlFndrInfo.fdType == 'cdev' &&
  52.             fi.fileParam.ioFlFndrInfo.fdCreator == 'mtcp') {
  53.             /* found the MacTCP driver file */
  54.     
  55.             /* save current volume (or working directory) */
  56.             pb.ioNamePtr = 0L;
  57.             if (PBHGetVol(&pb,false) != noErr)
  58.                 return(-1);
  59.             curVRefNum = pb.ioVRefNum;
  60.                 
  61.             /* move to system folder */
  62.             if (SetVol(NULL,info.sysVRefNum) != noErr)
  63.                 return(-1);
  64.             status = OpenResFile(&filename);
  65.             /* back to orignal volume */
  66.             SetVol(NULL,curVRefNum);
  67.             
  68.             return(status);
  69.             }
  70.         /* check next file in system folder */
  71.         fi.fileParam.ioFDirIndex++;
  72.         fi.fileParam.ioDirID = 0;
  73.         }
  74.     return(-1);
  75.     }    
  76.  
  77.  
  78. OSErr OpenResolver(fileName)
  79. char *fileName;
  80. {
  81.     short refnum;
  82.     OSErr rc;
  83.     
  84.     if (dnr != nil)
  85.         /* resolver already loaded in */
  86.         return(noErr);
  87.         
  88.     /* open the MacTCP driver to get DNR resources. Search for it based on
  89.        creator & type rather than simply file name */    
  90.     refnum = OpenOurRF();
  91.  
  92.     /* ignore failures since the resource may have been installed in the 
  93.        System file if running on a Mac 512Ke */
  94.        
  95.     /* load in the DNR resource package */
  96.     codeHndl = GetIndResource('dnrp', 1);
  97.     if (codeHndl == nil) {
  98.         /* can't open DNR */
  99.         return(ResError());
  100.         }
  101.     
  102.     DetachResource(codeHndl);
  103.     if (refnum != -1) {
  104.         CloseResFile(refnum);
  105.         }
  106.         
  107.     /* lock the DNR resource since it cannot be reloated while opened */
  108.     HLock(codeHndl);
  109.     dnr = (OSErrProcPtr) *codeHndl;
  110.     
  111.     /* call open resolver */
  112.     rc = (*dnr)(OPENRESOLVER, fileName);
  113.     if (rc != noErr) {
  114.         /* problem with open resolver, flush it */
  115.         HUnlock(codeHndl);
  116.         DisposHandle(codeHndl);
  117.         dnr = nil;
  118.         }
  119.     return(rc);
  120.     }
  121.  
  122.  
  123. OSErr CloseResolver()
  124. {
  125.     if (dnr == nil)
  126.         /* resolver not loaded error */
  127.         return(notOpenErr);
  128.         
  129.     /* call close resolver */
  130.     (void) (*dnr)(CLOSERESOLVER);
  131.  
  132.     /* release the DNR resource package */
  133.     HUnlock(codeHndl);
  134.     DisposHandle(codeHndl);
  135.     dnr = nil;
  136.     return(noErr);
  137.     }
  138.  
  139. OSErr StrToAddr(hostName, rtnStruct, resultproc, userDataPtr)
  140.     char *hostName;
  141.     struct hostInfo *rtnStruct;
  142.     long resultproc;
  143.     char *userDataPtr;
  144. {
  145.     if (dnr == nil)
  146.         /* resolver not loaded error */
  147.         return(notOpenErr);
  148.         
  149.     return((*dnr)(STRTOADDR, hostName, rtnStruct, resultproc, userDataPtr));
  150.     }
  151.     
  152. OSErr AddrToStr(addr, addrStr)
  153. unsigned long addr;
  154. char *addrStr;                                    
  155. {
  156.     if (dnr == nil)
  157.         /* resolver not loaded error */
  158.         return(notOpenErr);
  159.         
  160.     (*dnr)(ADDRTOSTR, addr, addrStr);
  161.     return(noErr);
  162.     }
  163.     
  164. OSErr EnumCache(resultproc, userDataPtr)
  165. long resultproc;
  166. char *userDataPtr;
  167. {
  168.     if (dnr == nil)
  169.         /* resolver not loaded error */
  170.         return(notOpenErr);
  171.         
  172.     return((*dnr)(ENUMCACHE, resultproc, userDataPtr));
  173.     }
  174.     
  175.     
  176. OSErr AddrToName(addr, rtnStruct, resultproc, userDataPtr)
  177. unsigned long addr;
  178. struct hostInfo *rtnStruct;
  179. long resultproc;
  180. char *userDataPtr;                                    
  181. {
  182.     if (dnr == nil)
  183.         /* resolver not loaded error */
  184.         return(notOpenErr);
  185.         
  186.     return((*dnr)(ADDRTONAME, addr, rtnStruct, resultproc, userDataPtr));
  187.     }
  188.